<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>智慧写作平台</title>
<link href="/pad/resources/css/bootstrap.min.css" rel="stylesheet">
<link href="./resources/css/star-rating.min.css" rel="stylesheet">
<link href="/pad/resources/css/style.css" rel="stylesheet">
</head>
<body>
	<nav class="navbar navbar-blue navbar-fixed-top" role="navigation">
		<div class="container bs-docs-container">
			<div class="navbar-header">
				<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-6">
					<span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span>
				</button>
				<h1>智慧写作平台</h1>
			</div>
			<ul class="nav navbar-nav">
				<li class="active"><a href="#">课程列表</a></li>
			</ul>
			<ul class="nav navbar-nav navbar-right">
				<li class="dropdown"><a id="user" href="#" class="dropdown-toggle" data-toggle="dropdown">用户 <span class="caret"></span></a>
					<ul class="dropdown-menu" role="menu">
						<li><a href="/pad/logout">退出登录</a></li>
					</ul></li>
			</ul>
		</div>
	</nav>
	
	
	    
	

		<div class="container bs-docs-container">
			<div id="pingfen"></div>
			<iframe id="editpad" src='#' width="100%" height="300" frameborder="no" border="0"></iframe>
		</div>		

	<script src="/pad/resources/js/jquery.min.js"></script>
		 <div id="cprtFooter">
	        <!-- 版权信息 -->
	 </div>	

<script type="text/javascript">
		$("#cprtFooter").load("copyrightFooter.html");
</script>
	<script src="/pad/resources/js/jquery.form.min.js"></script>
	<script src="/pad/resources/js/bootstrap.min.js"></script>
	<script src="/pad/resources/js/star-rating.min.js"></script>
	<script>
	
	function getQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg);
		return r?decodeURIComponent(r[2]):null;
	}
	
	 var asPId=getQueryString('assignmentPadId');
	$(function(){
		/*
		 * 传参，并且直接 查询反思维度  产生 打分按钮
		 */
		 
		 

			$.ajax({
				url : '/pad/getSessionUserB?assignmentPadId='+getQueryString('assignmentPadId'),
				success : function(user) {
					if(user.user.role=="teacher"){

						//1 问后台要数据 ，反思维度，assignmentid
						$.ajax({
							async: false,
							url:'/pad/reflection/getRftDimScore?assignmentPadId='+asPId,
							success:function(data){
								var htmlStr = "";
								var rftList = data.rftTs;
								var student=data.student;
								for(var i in rftList){
									htmlStr += '<button class="btn btn-info" data-toggle="modal" data-target="#commentModal'+i+'">给'+rftList[i].dimName+'维度评分</button> ';
									commentModal("commentModal"+i,student,data.assignmentPadId,rftList[i]);//产生评分 名字 按钮 并调用 下面写的  commentModal 方法
								}
								$('#pingfen').html(htmlStr);
							}
						});
					}
				}});
		 
		 
		 
		 
		}
	);
	
 function commentModal(modalId,student,assignmentPadId,rft){
		var commentModal ='<div id="'+modalId+'" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">';
		commentModal +='<div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>';
		commentModal +='<h4 class="modal-title" id="myModalLabel">给'+rft.dimName+'维度评分</h4></div><div class="modal-body">';
		commentModal +='<h5 class="modal-title" id="myModalLabel">'+rft.dimName+'维度总分为 '+rft.dimScore+',及格分数为 '+rft.passScore+'。请根据您的经验为  &nbsp&nbsp<font size="4" color="red"> '+student.userName+'</font>&nbsp&nbsp  同学在该维度上的反思打分</h5></div><div class="modal-body">';
		commentModal +='<form role="form" action="#">';
		commentModal +='<input type="hidden" name="studentId" value="'+student.userId+'">';
		commentModal +='<input type="hidden" name="dimId" value="'+rft.id+'">';
		commentModal +='<input type="hidden" name="assignmentPadId" value="'+asPId+'">';
		commentModal +='<input type="hidden" name="templateId" value="'+rft.templateId+'">';
		commentModal +='<div class="form-group">';
		commentModal +='<label><font size="4" color="red">评语:&nbsp</font></label>';
		commentModal +='<input type="text" style="height:30px;width:400px;" size="70" name="comment"\>';
		commentModal +='<input value="0" name="score" type="number" class="'+modalId+' rating form-control" min="0" max="'+rft.dimScore+'" step="1" data-stars="'+rft.dimScore+'" style="display: none;">';
		commentModal +='</div>';
		commentModal +='</form>';
		commentModal +='</div>';
		commentModal +='<div class="modal-footer">';
		commentModal +='<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>';
		commentModal +='<button type="button" class="btn btn-info" onclick="submitForm(\'#'+modalId+'\');">提交</button>';
		commentModal +='</div>';
		commentModal +='</div>';
		commentModal +='</div>';
		commentModal +='</div>';
		var assignmentModal = $("#"+modalId);
		if(assignmentModal.length>0)
			assignmentModal.replaceWith(commentModal);
		else
			$("html").append(commentModal);
		$("."+modalId).rating({
			step:1
		});
	}	
	 
 

	function submitForm(modalId){
	   var reflectionScore={};//提交到 后台的 json 数据
		$(modalId).find('form').each(function(){
			reflectionScore["templateId"]=this.templateId.value;
			reflectionScore["padId"]=this.assignmentPadId.value;
			reflectionScore["stuId"]=this.studentId.value;
			reflectionScore["comment"]=this.comment.value;
			reflectionScore["score"]=this.score.value;
			reflectionScore["rftDimId"]=this.dimId.value;
			reflectionScore["activityId"]=getQueryString('activityId');
			
		} );
		 
		 
		 $.ajax({
			   url: "/pad/reflection/saveReflectionScore",
			   type: 'POST',
			   data:reflectionScore,
			   async: false,
			   dataType: "json", //返回数据格式为json
			   success: function(data) {//请求成功完成后要执行的方法
				   //alert(data)
				   $(modalId).modal('hide');
			   }
		 });
		 
		 
	}
	
	
	
		$(function(){
		document.cookie="activityId="+getQueryString('activityId')+";path=/";

		
			$.ajax({
				url : '/pad/getSessionUserB?assignmentPadId='+getQueryString('assignmentPadId'),
				success : function(user) {
					//console.log(user);
					document.cookie="sessionID="+user.sessionID+";path=/"; 
					$("#editpad").attr("height", $(window).height() - 130);
						$.ajax({
							dataType:'jsonp',			
							jsonp:'jsonp',
							url:'http://localhost:8081/api/1.2.10/getReadOnlyID?apikey=e56b0cd06c86da040c24aeb3882cda37af9f7438a83d42d3aec923ca40219a52&padID='+getQueryString('assignmentPadId'),
							success:function(data){
								//console.log(data.data.readOnlyID);
								$("#editpad").attr(
										"src",
										"http://localhost:8081/p/"
												+ data.data.readOnlyID
												+ "?userName=" + user.user.realname);
							}
						});
					
					$("#user").html(user.user.realname);
				}
			});
		});
		
		
		function getCookie(name) {
			  var cookies = document.cookie;
			  var list = cookies.split("; ");          // 解析出名/值对列表
			      
			  for(var i = 0; i < list.length; i++) {
			    var arr = list[i].split("=");          // 解析出名和值
			    if(arr[0] == name)
			      return decodeURIComponent(arr[1]);   // 对cookie值解码
			  } 
			  return "";
			}
		
	</script>
</body>
</html>
